Skip to content

Method: generateInstances(Collection, String, int)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.test.environment;
19:
20: import cz.cvut.kbss.jopa.test.OWLClassA;
21: import cz.cvut.kbss.jopa.test.Vocabulary;
22:
23: import java.math.BigDecimal;
24: import java.math.BigInteger;
25: import java.net.MalformedURLException;
26: import java.net.URI;
27: import java.net.URL;
28: import java.time.LocalDate;
29: import java.time.OffsetDateTime;
30: import java.time.OffsetTime;
31: import java.time.temporal.ChronoUnit;
32: import java.util.*;
33: import java.util.stream.Collectors;
34:
35: /**
36: * Generators of test data.
37: */
38: public abstract class Generators {
39:
40: private static final int DEFAULT_MIN = 5;
41: private static final int DEFAULT_SIZE = 10;
42: private static final Set<String> TYPES = getTypes();
43:
44: private static final String PROPERTY_URI_BASE = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes#property";
45: private static final String TYPE_URI_BASE = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#Entity";
46:
47: private static final Random RANDOM = new Random();
48:
49: private Generators() {
50: // Private constructor
51: }
52:
53: public static List<OWLClassA> createSimpleList() {
54: return createSimpleList(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
55: }
56:
57: public static List<OWLClassA> createSimpleList(int size) {
58: assert size > 0;
59: final List<OWLClassA> lst = new ArrayList<>(size);
60: generateInstances(lst, "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityASimple",
61: size);
62: return lst;
63: }
64:
65: public static List<OWLClassA> createReferencedList() {
66: return createReferencedList(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
67: }
68:
69: public static List<OWLClassA> createReferencedList(int size) {
70: assert size > 0;
71: final List<OWLClassA> lst = new ArrayList<>(size);
72: generateInstances(lst,
73: "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityAReferenced", size);
74: return lst;
75: }
76:
77: public static List<LocalDate> createDataPropertyList() {
78: final List<LocalDate> list = new ArrayList<>(DEFAULT_SIZE);
79: for (int i = DEFAULT_SIZE; i >= 0; i--) {
80: list.add(LocalDate.now().minusDays(i));
81: }
82: return list;
83: }
84:
85: public static List<OWLClassA> createRDFCollection(int size) {
86: assert size > 0;
87: final List<OWLClassA> lst = new ArrayList<>(size);
88: generateInstances(lst,
89: "http://krizik.felk.cvut.cz/ontologies/jopa/tests/rdf-collection-item", size);
90: return lst;
91: }
92:
93: public static List<URI> createListOfIdentifiers() {
94: return createSimpleList().stream().map(OWLClassA::getUri).collect(Collectors.toList());
95: }
96:
97: public static Set<OWLClassA> createSimpleSet() {
98: return createSimpleSet(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
99: }
100:
101: public static Set<OWLClassA> createSimpleSet(int size) {
102: assert size > 0;
103: final Set<OWLClassA> set = new HashSet<>(size);
104: generateInstances(set, "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityASimpleSet",
105: size);
106: return set;
107: }
108:
109: public static Map<String, Set<String>> createProperties() {
110: return createProperties(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
111: }
112:
113: public static Map<String, Set<String>> createProperties(int size) {
114: assert size > 0;
115: final Map<String, Set<String>> m = new HashMap<>(size);
116: int counter = randomInt(1000);
117: for (int i = 0; i < size; i++) {
118: final Set<String> value = new HashSet<>(4);
119: for (int j = 0; j < size; j++) {
120: value.add("http://krizik.felk.cvut.cz/ontologies/jopa/tests/ObjectPropertyValue_" + j + "_"
121: + counter);
122: }
123: m.put(PROPERTY_URI_BASE + counter, value);
124: counter++;
125:
126: }
127: return m;
128: }
129:
130: public static Map<URI, Set<Object>> createTypedProperties() {
131: return createTypedProperties(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
132: }
133:
134: public static Map<URI, Set<Object>> createTypedProperties(int size) {
135: assert size > 0;
136: final Map<URI, Set<Object>> props = new HashMap<>(size);
137: int counter = randomInt(1000);
138: for (int i = 0; i < size; i++) {
139: final Set<Object> value = new HashSet<>();
140: for (int j = 0; j < size; j++) {
141: // Generate either an individual's URI or random data value. But same type for a property
142: // (so that the property is either object or data, but not both)
143: if (counter % 2 == 0) {
144: value.add(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/Property_" + counter +
145: "Individual_" + j));
146: } else {
147: value.add(generateRandomPropertyValue(j, counter));
148: }
149: }
150: props.put(URI.create(PROPERTY_URI_BASE + counter), value);
151: counter++;
152: }
153: return props;
154: }
155:
156: private static Object generateRandomPropertyValue(int valueIndex, int propertyIndex) {
157: final int random = randomInt(10);
158: return switch (random) {
159: case 0 -> // boolean
160: valueIndex % 2 == 0;
161: case 1 -> // int
162: valueIndex;
163: case 2 -> // long
164: System.currentTimeMillis();
165: case 3 -> //double
166: ((double) propertyIndex + 1) / (valueIndex + 1);
167: case 4 -> // datetime
168: // Generate date rounded to milliseconds to prevent issues with time rounding
169: OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS);
170: case 5 -> OffsetTime.now().truncatedTo(ChronoUnit.MILLIS);
171: case 6 -> LocalDate.now();
172: case 7 -> // String
173: "TypedProperty_" + propertyIndex + "Value_" + valueIndex;
174: case 8 -> BigInteger.valueOf(valueIndex);
175: case 9 -> BigDecimal.valueOf(Math.PI);
176: default -> throw new IllegalArgumentException();
177: };
178: }
179:
180: private static void generateInstances(Collection<OWLClassA> col, String uriBase, int size) {
181:• assert size > 0;
182: int counter = randomInt(1000);
183:• for (int i = 0; i < size; i++) {
184: final OWLClassA a = new OWLClassA();
185: a.setUri(URI.create(uriBase + counter));
186: a.setStringAttribute("stringAttributeeee" + counter);
187: counter++;
188: a.setTypes(TYPES);
189: col.add(a);
190: }
191: }
192:
193: public static OWLClassA generateOwlClassA() {
194: final OWLClassA a = new OWLClassA(generateUri());
195: a.setStringAttribute("String attribute " + randomInt(10000));
196: a.setTypes(TYPES);
197: return a;
198: }
199:
200: private static Set<String> getTypes() {
201: final Set<String> types = new HashSet<>(3);
202: types.add(Vocabulary.CLASS_IRI_BASE + "OWLClassDF");
203: types.add(Vocabulary.CLASS_IRI_BASE + "OWLClassDFF");
204: types.add(Vocabulary.CLASS_IRI_BASE + "OWLClassDFFF");
205: return types;
206: }
207:
208: public static Set<URL> createUrls() {
209: return Generators.createSimpleList().stream().map(a -> {
210: try {
211: return a.getUri().toURL();
212: } catch (MalformedURLException e) {
213: throw new IllegalArgumentException(e);
214: }
215: }).collect(Collectors.toSet());
216: }
217:
218: public static int randomInt() {
219: return RANDOM.nextInt();
220: }
221:
222: public static int randomInt(int max) {
223: return RANDOM.nextInt(max);
224: }
225:
226: /**
227: * Gets a random int between {@code min} and {@code max}.
228: *
229: *
230: * @param min lower bound (inclusive)
231: * @param max upper bound (exclusive)
232: * @return Random positive integer
233: */
234: public static int randomPositiveInt(int min, int max) {
235: assert min >= 0;
236: if (max <= min) {
237: throw new IllegalArgumentException("Upper bound has to be greater than the lower bound.");
238: }
239: int rand;
240: do {
241: rand = RANDOM.nextInt(max);
242: } while (rand < min);
243: return rand;
244: }
245:
246: public static boolean randomBoolean() {
247: return RANDOM.nextBoolean();
248: }
249:
250: public static Set<URI> createUriTypes() {
251: final int count = randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE);
252: final Set<URI> result = new HashSet<>();
253: for (int i = 0; i < count; i++) {
254: result.add(URI.create(TYPE_URI_BASE + randomInt(Integer.MAX_VALUE)));
255: }
256: return result;
257: }
258:
259: /**
260: * Generates a random URI.
261: *
262: * @return Random URI
263: */
264: public static URI generateUri() {
265: return URI.create(Vocabulary.INDIVIDUAL_IRI_BASE + randomInt(Integer.MAX_VALUE));
266: }
267:
268: /**
269: * Gets random item from the specified list.
270: * @param items List of items
271: * @return Random item from the list
272: */
273: public static <T> T getRandomItem(List<T> items) {
274: return items.get(randomPositiveInt(0, items.size()));
275: }
276:
277: public static Random getRandomGenerator() {
278: return RANDOM;
279: }
280: }